package com.faithbj.printer.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.faithbj.printer.model.ExcelElement;
import com.faithbj.printer.service.FilePrinter;
import com.faithbj.printer.service.Impl.FilePrinterImpl;

public class ExcelUtil {
	
	/**
	 * 修改excel(2003)
	 * @param filepath
	 * @param elements
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void modifyExcel(String filepath, List<ExcelElement> elements) throws FileNotFoundException, IOException {
		FileInputStream fis = new FileInputStream(filepath);
		Workbook wb =  new HSSFWorkbook(fis);
		Sheet sheet = wb.getSheetAt(0); 
		for(ExcelElement element:elements){
			Cell cell = sheet.getRow(element.getRow()).getCell(element.getCell());
			cell.setCellValue(element.getValue());
		}
		
		FileOutputStream fileOut = new FileOutputStream(filepath);
		wb.write(fileOut);
		fis.close();
		fileOut.close();
	}

	public static void printExcel(List<ExcelElement> elements) throws FileNotFoundException, IOException{
		String filepath = "D:/printer/template.xls";
		modifyExcel(filepath,elements);
		FilePrinter fpi = new FilePrinterImpl();
		fpi.printExcel(filepath);
	}

}
